package com.chen.controller;

import com.chen.model.Contract;
import com.chen.model.Invoice;
import com.chen.model.Salesman;
import com.chen.service.ContractService;
import com.chen.service.InvoiceService;
import com.chen.service.SalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/salesman")
public class SalesmanController {
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private InvoiceService invoiceService;

//    @RequestMapping("/test")
//    public void test(){
//        Salesman salesman = new Salesman();
//        salesman.setSalesman_name("xiaogege");
//        salesman.setSalesman_account("123");
//        salesman.setSalesman_password("1");
//        System.out.println(salesman);
//        salesmanService.addSalesman(salesman);
//        salesman.setSalesman_password("666");
//        System.out.println(salesman);
//        salesmanService.updateSalesman(salesman);
//        List<Salesman> salesmanList;
//        salesmanList = salesmanService.findAllSalesman();
//        System.out.println(salesmanList.get(0));
//        System.out.println(salesmanList.toString());
//        salesman=salesmanService.findSalesmanById(1);
//        System.out.println(salesman);
//        salesmanList = salesmanService.findSalesmanByName("xiaogege");
//        System.out.println(salesmanList.toString());
//    }

    @RequestMapping("/salesman-add")
    public String salesmanAdd(){
        return "salesman-add";
    }

    @RequestMapping("/addSalesman")
    public String addSalesman(Salesman salesman){
        salesmanService.addSalesman(salesman);
        return "success";
    }

    @RequestMapping("/salesman-edit")
    public String customerEdit(Integer salesman_id,Model model){
        Salesman salesman = salesmanService.findSalesmanById(salesman_id);
        model.addAttribute("salesman",salesman);
        return "salesman-edit";
    }

    @RequestMapping("/updateSalesman")
    public String updateSalesman(Salesman salesman){
//        Customer customer = customerService.findCustomerById(customer_id);
        System.out.println(salesman);
        salesmanService.updateSalesman(salesman);
        return "success";
    }

    @RequestMapping("/findAllSalesman")
    public String findAllSalesman(Model model){
        List<Salesman> salesmanList;
        salesmanList = salesmanService.findAllSalesman();
        for(Salesman salesman:salesmanList){
            System.out.println(salesman);
        }
        model.addAttribute("salesmanList",salesmanList);
        return "salesman-list";
    }

    @RequestMapping("/findAllContract")
    public String findAllContract(Model model, HttpSession session){
        Salesman salesman = (Salesman) session.getAttribute("salesman");
        System.out.printf(salesman.toString());
        List<Contract> contractList = contractService.findAllContract();
        System.out.println(contractList.toString());
        for (Contract contract :contractList){
            List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
            int state = 2;
            for (Invoice invoice:invoiceList){
                if(invoice.getInvoice_state()==1){
                    state=1;
                    break;
                }
            }
            if(state==1) {
                contract.setContract_state(state);
                contractService.updateState(contract);
                continue;
            }
            for (Invoice invoice:invoiceList){
                if(invoice.getInvoice_state()!=2){
                    state=0;
                    break;
                }
            }
            if(state==0){
                contract.setContract_state(state);
                contractService.updateState(contract);
                continue;
            }
            else {
                contract.setContract_state(state);
                contractService.updateState(contract);
            }
//            System.out.println(contract);
        }
        contractList = contractService.findAllContract();
        List<Contract> contractList1 = new ArrayList<>();
        for(Contract contract:contractList){
            System.out.println(contract.getSalesman().getSalesman_id());
            System.out.println(salesman.getSalesman_id());
            if(contract.getSalesman().getSalesman_id()==salesman.getSalesman_id()){
                System.out.println("00000000000001111111111");
                contractList1.add(contract);
            }
        }
        model.addAttribute("contractList",contractList1);
        return "salesman-contract";
    }

    @RequestMapping("/contract_check")
    public String contract_check(Model model,Integer contract_id){
        List<Invoice> invoiceList;
        System.out.println("111");
        System.out.println(contract_id.toString()+"111");
//        invoiceList= invoiceService.findInvoiceByContract(contract_id);
        invoiceList= invoiceService.findInvoiceByContract(contract_id);
//        invoiceList= invoiceService.findAllInvoice();
        System.out.println(invoiceList.toString());
        model.addAttribute("invoiceList",invoiceList);
        return "contract-check";
    }
    @RequestMapping("/findAllStatisticsTotal")
    public String findAllStatisticsTotal(Model model , HttpServletRequest request ,HttpSession session) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        Salesman salesman = (Salesman) session.getAttribute("salesman");
        List<Contract> contractList2 = contractService.findAllContract();
        List<Contract> contractList = new ArrayList<>();
        for (Contract contract:contractList2){
            if(contract.getSalesman().getSalesman_id()==salesman.getSalesman_id()){
                contractList.add(contract);
            }
        }
        List<Integer> totalList = new ArrayList<Integer>();
//        if(!date1.equals("")&&!date2.equals("")) {
//        Date utilDate = sdf.parse(time1);
        if(date1==null||date2==null){
            for (Contract contract : contractList) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            Integer contract_total = 0;
            for (Integer integer : totalList) {
                contract_total += integer;
            }
            model.addAttribute("contract_total", contract_total);
            model.addAttribute("contractList", contractList);
            model.addAttribute("totalList", totalList);
        }
        else {
            Date time1 = sdf.parse(date1);
            Date time2 = sdf.parse(date2);
            List<Contract> contractList1 = new ArrayList<Contract>();
            for (Contract contract:contractList){
                if(!contract.getContract_time().before(time1)&&!contract.getContract_time().after(time2)){
                    contractList1.add(contract);
                }
            }
            for (Contract contract : contractList1) {
                List<Invoice> invoiceList = invoiceService.findInvoiceByContract(contract.getContract_id());
                Integer total = 0;
                for (Invoice invoice : invoiceList) {
                    total += invoice.getGoods_number() * invoice.getGoods_price();
                    System.out.println(total);
                }
                totalList.add(total);
                System.out.println(totalList.toString());
            }
            Integer contract_total = 0;
            for (Integer integer : totalList) {
                contract_total += integer;
            }
            model.addAttribute("contract_total", contract_total);
            model.addAttribute("contractList", contractList1);
            model.addAttribute("totalList", totalList);
        }
        return "statistics-total";
    }

    @RequestMapping("/findSalesmanById")
    public String  findSalesmanById(Integer salesman_id,Model model){
        Salesman salesman = salesmanService.findSalesmanById(salesman_id);
        List<Salesman> salesmanList = new ArrayList<>();
        if (salesman!=null){
            salesmanList.add(salesman);
        }
        model.addAttribute("salesmanList",salesmanList);
        return "salesman-list";
    }
}
